;
(function (call) {
    var main = {
        exponentCfg: {
            'TT': '总指数', 'KI': 'K指数', 'LI': '抬升指数', 'SI': '沙氏指数', 'H-20C': '-20度层高度', 'H0C': '零度高度', 'TD2M': '2m露点',
            'TMP2M': '2m气温', 'Q700': '比温700', 'Q850': '比温850', 'SWEAT': '强天气威胁指数', 'CAPE': '对流有效位能', 'CIN': '对流抑制有效位能',
            '700-500TMP': '700-500温差', '850-500TMP': '850-500温差'
        },
        router: {
            queryCnvciHailNdex: '/zhfx/warnSet/queryCnvciHailNdex',
            updateCnvciHailNdex: '/zhfx/warnSet/updateCnvciHailNdex'
        },
        init: function () {
            this.initMonthSelect();
            this.queryCnvciHailNdex();
            this.bindEvt();
        },
        initMonthSelect: function () {
            var month = ['4', '5', '6', '7', '8', '9'];
            var curMonth = new Date().format('MM');
            var $select = $("#monthSelect").empty();
            for (var i = 0; i < month.length; i++) {
                if (Number(curMonth) == month[i]) {
                    $select.append(`<option selected value="${month[i]}">${month[i]}</option>`)
                } else {
                    $select.append(`<option value="${month[i]}">${month[i]}</option>`)
                }
            }
        },
        // 查询
        queryCnvciHailNdex: function () {
            var that = this;
            var $body = $('.table-right tbody tr').empty();
            var params = {
                month: $("#monthSelect").val(),
                type: $("#indexSelect").val()
            }
            $.getJSON(that.router.queryCnvciHailNdex, params, function (res) {
                if (res.isSuccess) {
                    var dataArr = res.data;
                    for (var i = 0; i < dataArr.length; i++) {
                        var relation = dataArr[i].relation === 0 ? '>' : dataArr[i].relation == 1 ? '<' : '<>';
                        var td = '<td><span class="element" element="' + dataArr[i].element + '">' + that.exponentCfg[dataArr[i].element] + '</span>'
                        td += '<span class="relation" relation="' + dataArr[i].relation + '">' + relation + '</span>'
                        td += '<input class="avgIndex" type="number" value="' + dataArr[i].avgIndex + '">'
                        td += '<input class="std" type="number" value="' + dataArr[i].std + '"></td>'
                        var $td = $(td);
                        $body.append($td);
                    }
                }
            })
        },
        // 保存
        updateCnvciHailNdex: function () {
            var that = this;
            var data = [];
            $(".table-right tbody tr td").each(function (i, td) {
                var obj = {};
                obj.month = $("#monthSelect").val();
                obj.type = $("#indexSelect").val();
                obj.element = $(td).find('.element').attr('element');
                obj.relation = $(td).find('.relation').attr('relation');
                obj.avgIndex = $(td).find('.avgIndex').val();
                obj.std = $(td).find('.std').val();
                obj.maxIndex = 0;
                obj.minIndex = 0;
                data.push(obj);
            })
            var params = { data: data };
            $.ajax({
                url: that.router.updateCnvciHailNdex,
                type: "post",
                contentType: "application/json;charset=utf-8",
                dataType: 'json',
                data: JSON.stringify(params),
                success: function (res) {
                    if (res.isSuccess) {
                        alert('保存成功');
                    } else {
                        alert('保存失败');
                    }
                }
            })
        },
        bindEvt: function () {
            var that = this;
            $("#saveBtn").click(function () {
                that.updateCnvciHailNdex();
            })
            $("#refreshBtn").click(function () {
                debugger
            })
            $("#indexSelect").change(function () {
                that.queryCnvciHailNdex();
            })
            $("#monthSelect").on('change', function () {
                that.queryCnvciHailNdex();
            })
        }
    };

    if (typeof call === 'function') {
        call(main);
    }

})(function (main) {
    main.init();
});